//
//  OrderOptionsViewController.m
//  MyPizzaMobile
//
//  Created by Anton Atanasov on 16.02.13.
//  Copyright (c) 2013 г. Stanislav. All rights reserved.
//

#import "OrderOptionsViewController.h"

@interface OrderOptionsViewController ()
{
    BOOL pickerBackgroundNotHidden;
}
@property NSArray * pizzaSizes;
@property(readwrite) RequestManager * requestManager;
@end

@implementation OrderOptionsViewController
@synthesize pizzaSizes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    pickerBackgroundNotHidden = YES;
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString* addressText = [defaults objectForKey:@"ordersAddress"]!=nil?[defaults objectForKey:@"ordersAddress"]:[defaults objectForKey:@"address"];
    NSString *strippedContent = [addressText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.orderAddressTextField.text = strippedContent;
    self.navigationItem.title = @"Order Options";
    self.pizzaSizes = [[NSArray alloc]initWithObjects:[[NSNumber alloc]initWithInt:18],
                       [[NSNumber alloc]initWithInt:22],
                       [[NSNumber alloc]initWithInt:26],
                       [[NSNumber alloc]initWithInt:30],
                       [[NSNumber alloc]initWithInt:40],
                       [[NSNumber alloc]initWithInt:50],
                       [[NSNumber alloc]initWithInt:60], nil];
    self.requestManager = [RequestManager requestManagerInstance];
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return pizzaSizes.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerBackgroundNotHidden)
    {
        pickerBackgroundNotHidden = NO;
        [(UIView*)[[pickerView subviews] objectAtIndex:0] setHidden:YES];
        [(UIView*)[[pickerView subviews] objectAtIndex:7] setHidden:YES];
        [(UIView*)[[pickerView subviews] objectAtIndex:8] setHidden:YES];
    }
    return [[NSString alloc]initWithFormat:@"%@см.", [pizzaSizes objectAtIndex:row]];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setPizzaSizePicker:nil];
    [self setOrderAddressTextField:nil];
    [super viewDidUnload];
}
- (IBAction)nextButtonPressed:(UIBarButtonItem *)sender {
    [self performSegueWithIdentifier:@"chooseSupplements" sender:self];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.orderAddressTextField resignFirstResponder];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqual: @"chooseSupplements"])
    {
        NSInteger row = [self.pizzaSizePicker selectedRowInComponent:0];
        [self.requestManager.orderPizzas setObject:[self.pizzaSizes objectAtIndex:row] forKey:@"size"];
        [self.requestManager.orderPizzas setObject:self.self.orderAddressTextField.text forKey:@"orderAddress"];
    }
}

- (IBAction)dissmissKeyboard:(UITextField *)sender {
}
@end
